/**
 * Created by Administrator on 2017/3/21.
 */
import React, {Component} from 'react';
import {Form, Input, Radio, Select, Switch, InputNumber} from 'antd';
import {connect} from 'dva';
import request from '@/utils/request';
const FormItem = Form.Item;
const Option = Select.Option;
@connect(({roles}) => ({
  roles
}))
class AddPoliciesForm extends Component {
  constructor(props) {
    super(props);
    this.state = {
      showCustom: false
    };
  }

  componentDidMount() {
  this.fetch(1)

  }
  fetch=(level)=>{
    const that=this
    request(`/admin/level_commissions`, {
      method: 'GET',
      params: {
        project_id:this.props.project_id
      },
    }).then((response)=>{
      if(response.status===200){
        if(level==1){
          that.props.form.setFieldsValue({
            initial_commission:response.data.data.level1_initial_commission
          })
        }
        if(level==2){
          that.props.form.setFieldsValue({
            initial_commission:response.data.data.level2_initial_commission
          })
        }

      }
    });
  }
  render() {
    const formItemLayoutWithLabel = {
      labelCol: {
        xs: {span: 24},
        sm: {span: 7},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 15},
      }
    };

    const {getFieldDecorator} = this.props.form;
    return (
      <div>
        <Form onSubmit={this.handleSubmit}>
          <FormItem
            {...formItemLayoutWithLabel}
            label={(
              <span>
                层级
            </span>
            )}
          >
            {getFieldDecorator('level', {
              initialValue:{label:'层级一',key:'1'},
              rules: [{required: true}],
            })(
              <Select onChange={(e)=>{
                  this.fetch(e.key)
              }}  labelInValue={true}>
                { [{name:'层级一',id:'1'},{name:'层级二',id:'2'}].map((item, key) => {
                  return (
                    <Option key={item.id}
                            value={item.id}>{item.name}</Option>
                  )
                }) }
              </Select>
            )}
          </FormItem>
          <FormItem
            {...formItemLayoutWithLabel}
            label={(
              <span>
               初始佣金(元)
            </span>
            )}
          >
            {getFieldDecorator('initial_commission', {
              initialValue: '',
              rules: [{required: true,message: '初始佣金不能为空'}],
            })(
              <InputNumber min={0} style={{width:'100%'}}/>
            )}
          </FormItem>

        </Form>
      </div>
    );
  }
}

const AddPoliciesFormWrap = Form.create()(AddPoliciesForm);
export default connect()(AddPoliciesFormWrap);


